/**
 * oyt
 */
package com.suliao.home.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.suliao.home.entity.TokenSession;
import com.suliao.home.service.AuthSessionService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.UUID;

/**
 * <p>
 * 
 * </p>
 * <font size=0.25>Copyright (C) 2018 Ouyeel. All Rights Reserved.</font>
 * @author Tao.Ouyang (Create on:2018年7月6日)
 * @version 1.0
 * @fileName AuthSessionServiceImpl.java
 */
@Service
public class AuthSessionServiceImpl implements AuthSessionService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public static final String            USER_KEY  = "userMap";

    public static final String            TOKEN_KEY = "tokenMap";

    /**
     * 保存用户信息到redis
     * */
    @Override
    public String cacheSession(String userName) {
        //创建token
        String token = UUID.randomUUID().toString().replace("-","");
        if (getUserInfo(userName) != null) {
            deleteUserToken(userName);
        }
        putUserInfo(userName, token);//保存用户与Session关系
        TokenSession tokenSession = new TokenSession(token, userName);
        putTokenInfo(token, tokenSession);//保存session与用户信息的换下

        return token;
    }

    @Override
    public boolean checkAndAddAddress(String token, String address) {
        TokenSession tokenSession = getTokenInfo(token);
        if (tokenSession != null) {
            tokenSession.getAddressList().add(address);
            tokenSession.setTokenFlag(true);
            putTokenInfo(token, tokenSession);
            return true;
        }
        return false;
    }

    @Override
    public boolean checkUserLoginStatus(String userName) {
        boolean flag = false;
        String token = getUserInfo(userName);
        if (token != null) {
            TokenSession tokenSession = getTokenInfo(token);
            if (tokenSession != null) {
                flag = true;
            }
        }
        return flag;
    }

    @Override
    public String getUserToken(String userName) {
        String token = getUserInfo(userName);
        if (token == null) {
            return null;
        }
        else {
            if (getTokenInfo(token) != null) {
                return token;
            }
            else {
                return null;
            }

        }
    }

    @Override
    public void logoutByUser(String userName) {
        deleteUserInfo(userName);
    }

    @Override
    public void logoutByToken(String ssoToken) {
        if (ssoToken != null) {
            TokenSession tokenSession = getTokenInfo(ssoToken);
            if (tokenSession != null) {
                deleteTokenInfo(ssoToken);
            }
        }
    }

    public void putUserInfo(String userName, String token) {
        HashOperations<String, String, Object> hashUserOp = redisTemplate.opsForHash();
        hashUserOp.put(USER_KEY, userName, token);

    }

    public void deleteUserInfo(String userName) {
        HashOperations<String, String, Object> hashUserOp = redisTemplate.opsForHash();
        hashUserOp.delete(USER_KEY, userName);
    }

    public void putTokenInfo(String tokenKey, TokenSession tokenSession) {
        HashOperations<String, String, Object> hashTokenOp = redisTemplate.opsForHash();

        String jsonstr = JSON.toJSONString(tokenSession);

        hashTokenOp.put(TOKEN_KEY, tokenKey, jsonstr);

    }

    public void deleteTokenInfo(String tokenKey) {
        HashOperations<String, String, Object> hashTokenOp = redisTemplate.opsForHash();
        hashTokenOp.delete(TOKEN_KEY, tokenKey);

    }

    public String getUserInfo(String userName) {
        HashOperations<String, String, Object> hashUserOp = redisTemplate.opsForHash();
        return (String) hashUserOp.get(USER_KEY, userName);
    }

    public TokenSession getTokenInfo(String tokenKey) {
        HashOperations<String, String, Object> hashTokenOp = redisTemplate.opsForHash();
        JSONObject jsobj = JSON.parseObject(hashTokenOp.get(TOKEN_KEY, tokenKey).toString());
        //json转换为对象
        TokenSession tokenSession = jsobj.toJavaObject(TokenSession.class);
        return tokenSession;
    }
    
    @Override
    public void deleteUserToken(String userName) {

        HashOperations<String, String, String> hashOp = redisTemplate.opsForHash();
        String token = hashOp.get(USER_KEY, userName);
        if (token != null) {
            hashOp.delete(USER_KEY, userName);
            hashOp.delete(TOKEN_KEY, token);
        }
    }

    @Override
    public TokenSession getUserTokenInfo(String userName) {
        HashOperations<String, String, String> hashOp = redisTemplate.opsForHash();
        String token = hashOp.get(USER_KEY, userName);
        JSONObject jsobj = JSON.parseObject(hashOp.get(TOKEN_KEY, token).toString());
        //json转换为对象
        TokenSession tokenSession = jsobj.toJavaObject(TokenSession.class);
        return tokenSession;
    }

}
